var $;
layui.config({
	base : "js/"
}).use(['form','layer','jquery'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage;
		$ = layui.jquery;

 	var addUserArray = [],addUser = [];
 	form.on("submit(addUser)",function(data){
 		//是否添加过信息.
	 	/*if(window.sessionStorage.getItem("addUser")){
	 		addUserArray = JSON.parse(window.sessionStorage.getItem("addUser"));
	 	}*/

	 	var userStatus;
 		//会员状态
 		if(data.field.userStatus == '0'){
 			userStatus = "0";
 		}else if(data.field.userStatus == '1'){
 			userStatus = "1";
 		}

 		addUser = '{"username":"'+ $('.userName').val() + '","password":"' + $('.userPwd').val() + '","email":"'+ $('.userEmail').val() +'","mobile":"'+ $('.Mobile').val() +'","status":"'+userStatus  +'","dept_id":"1","create_time":"'+ formatTime(new Date()) +'","update_time":"'+formatTime(new Date())+'"}';

        $.ajax({
            url : "http://localhost/ci/admin/user/imp_add",
            type : "post",
            data : {'data':addUser, 'token':localStorage.token,'user_token':localStorage.user},
            dataType : "json",
            success : function(data){
            	console.log(data);
                //top.layer.close(index);
                top.layer.msg("用户添加成功！");
               layer.closeAll("iframe");
                location.href="http://localhost/ci/admin/user";
                //刷新父页面
                parent.location.reload();

            },
            error : function (data) {
            	console.log(data);
                //top.layer.close(index);
                top.layer.msg("用户添加失败！");
                layer.closeAll("iframe");
                location.href="http://localhost/ci/admin/user";
                //刷新父页面
                //parent.location.reload();
            }
        })
 		return false;
 	})
	
})

//格式化时间
function formatTime(_time){
    var year = _time.getFullYear();
    var month = _time.getMonth()+1<10 ? "0"+(_time.getMonth()+1) : _time.getMonth()+1;
    var day = _time.getDate()<10 ? "0"+_time.getDate() : _time.getDate();
    var hour = _time.getHours()<10 ? "0"+_time.getHours() : _time.getHours();
    var minute = _time.getMinutes()<10 ? "0"+_time.getMinutes() : _time.getMinutes();
    return year+"-"+month+"-"+day+" "+hour+":"+minute;
}
